"""
    @author yu_chen
    @date  
    @todo 
"""

import requests
from lxml import etree

headers = {
    'Referer': 'http://music.163.com/',
    'Host': 'music.163.com',
    'Accept-Language': "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
    'Accept-Encoding': "gzip, deflate",
    'Content-Type': "application/x-www-form-urlencoded",
    'Origin': 'https://music.163.com',
    'Connection': "keep-alive",
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36'
                  ' (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
}
for i in range(10):
    url = "https://music.163.com/discover/playlist/?order=hot&cat=%E5%85%A8%E9%83%A8&limit=35&offset="
    html = requests.get(url + str(i * 35), headers=headers)
    text = etree.HTML(html.text)
    songs_list_names = text.xpath('//ul[@id="m-pl-container"]/li//a[@class="msk"]/@title')
    # 获取歌曲id
    songs_list_ids = text.xpath('//ul[@id="m-pl-container"]/li//a[@class="msk"]/@href')
    songs_list_ids = [s_id[13:] for s_id in songs_list_ids]

    for j in range(len(songs_list_ids)):
        line = songs_list_ids[j] + ',' + songs_list_names[j] + '\n'
        filename = '../resource/all_songs_list_ids.txt'
        with open(filename, 'a') as f:
            f.writelines(line)
